﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OS.BLL;

namespace DownloadPlug_ins
{
    public partial class frmImport : Form
    {
        public frmImport()
        {
            InitializeComponent();
        }

        private void pSet_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.ShowDialog();
        }

        private void gbtnQuery_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开";
            //ofd.Filter = "*.xlsx|*.xls";
            ofd.Filter = "Excel2003(*.xls)|*.xls|Excel2007(*.xlsx)|*.xlsx";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.ctxtFileName.Text = ofd.FileName;
            }
        }

        private void gbtnImport_Click(object sender, EventArgs e)
        {
           
            int type0 = 1;
            #region 导入景区人员信息
            if (type0 == 0)
            {
                string filename = this.ctxtFileName.Text.Trim();
                DataTable dt = NPOIHelper.Import(filename, 0, 0, 0);
                int count1 = 0;
                int count2 = 0;
                UserArchives_Entity entity = new UserArchives_Entity();
                Bll_UserArchives bllUserArchives = new Bll_UserArchives();
                Bll_Departments departmentsBLL = new Bll_Departments();
                try
                {
                    string typeName = dt.TableName;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[0] != null && dr[0].ToString() != "")
                        {
                            string id = dr[0].ToString();
                            entity.Id = id;
                            string name = dr[1].ToString();
                            entity.Name = name;
                            string sex = dr[2].ToString();
                            if (sex == "男")
                                entity.Sex = "1";
                            else if (sex == "女")
                                entity.Sex = "2";
                            else
                                entity.Sex = sex;
                            string birthday = dr[3].ToString();
                            if (birthday != "")
                                entity.Birthday = DateTime.Parse(birthday);
                            string deptCode = dr[4].ToString();
                            string deptName = dr[5].ToString();
                            entity.dept = departmentsBLL.GetDepartmentsIDByName(deptName);
                            string siren = dr[6].ToString();
                            entity.Siren = siren;
                            string isLeader = dr[7].ToString();
                            if (isLeader == "是")
                                entity.IsLeader = 1;
                            else
                                entity.IsLeader = 0;
                            string idNumber = dr[8].ToString();
                            entity.IDNumber = idNumber;

                            if (birthday == "" && idNumber != "")
                            {
                                if (idNumber.Length == 18)
                                {
                                    string year = idNumber.Substring(6, 4);
                                    string mm = idNumber.Substring(10, 2);
                                    string dd = idNumber.Substring(12, 2);

                                    DateTime ndt = new DateTime(int.Parse(year), int.Parse(mm), int.Parse(dd));
                                    entity.Birthday = ndt;
                                }
                            }
                            string type = dr[9].ToString();
                            int value = 0;
                            if (type == "民警")
                                value = 1;
                            else if (type == "武警")
                                value = 2;
                            else
                                value = 3;
                            entity.Type = value;
                            string office = dr[10].ToString();
                            entity.Office = office;
                            string responsibilities = dr[11].ToString();
                            entity.Responsibilities = responsibilities;
                            string tel = dr[12].ToString();
                            entity.Tel = tel;
                            string mobilePhone = dr[13].ToString();
                            entity.MobilePhone = mobilePhone;
                            string virtualNumber = dr[14].ToString();
                            entity.VirtualNumber = virtualNumber;
                            string email = dr[15].ToString();
                            entity.Email = email;
                            string roomNumber = dr[16].ToString();
                            entity.RoomNumber = roomNumber;
                            string diskSize = dr[17].ToString();
                            if (diskSize != "")
                                entity.DiskSize = int.Parse(diskSize);
                            string deptTelephone = dr[18].ToString();
                            entity.DeptTelephone = deptTelephone;
                            string address = dr[19].ToString();
                            entity.Address = address;
                            if (id == "用户编号" && name == "用户姓名")
                            {
                            }
                            else
                            {
                                if (!bllUserArchives.Exists(entity.Id))
                                {
                                    //导入
                                    if (bllUserArchives.Add(entity))
                                        count1++;
                                }
                                else
                                {
                                    //导入
                                    if (bllUserArchives.Update(entity))
                                        count2++;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex) { }
                MessageBox.Show("共计添加用户记录:" + count1 + "条，更新用户记录:" + count2);
            }
            #endregion

            Bll_Departments bllDepartments = new Bll_Departments();
            Bll_WorkGroup bllWorkGroup = new Bll_WorkGroup();

            List<Departments_Entity> ls = bllDepartments.GetModelList("");

            int count = 0;
            foreach (Departments_Entity entity in ls)
            {
                WorkGroup_Entity model = new WorkGroup_Entity();
                model.PWorkGroupId = entity.ParentId.ToString();
                model.SortNum = 1;
                model.WorkGroupId = entity.DeptId.ToString();
                model.WorkGroupIsShow = 1;
                model.WorkGroupName = entity.DeptName;
                model.WorkGroupRemark = "";

                if (bllWorkGroup.Add(model))
                {
                    count++;
                }
            }
            MessageBox.Show("导入部门成功！");

        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            string filename = this.ctxtFileName.Text.Trim();

            DataTable dt = NPOIHelper.Import(filename, 0, 0, 0);

            int count1 = 0;
            int count2 = 0;
            UserArchives_Entity entity = new UserArchives_Entity();
            Bll_UserArchives bllUserArchives = new Bll_UserArchives();
            Bll_Departments departmentsBLL = new Bll_Departments();

            Bll_UserList bllUserList = new Bll_UserList();
            Bll_WorkGroupUserList bllWorkGroupUserList = new Bll_WorkGroupUserList();


            try
            {

                string typeName = dt.TableName;

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[0] != null && dr[0].ToString() != "")
                    {
                        string id = dr[0].ToString();
                        entity.Id = id;
                        string name = dr[1].ToString();
                        entity.Name = name;
                        string sex = dr[2].ToString();

                        if (sex == "男")
                            entity.Sex = "1";
                        else if (sex == "女")
                            entity.Sex = "2";
                        else
                            entity.Sex = sex;
                        string birthday = dr[3].ToString();
                        if (birthday != "")
                            entity.Birthday = DateTime.Parse(birthday);


                        string deptCode = dr[4].ToString();
                        string deptName = dr[5].ToString();

                        entity.dept = departmentsBLL.GetDepartmentsIDByName(deptName);
                        string siren = dr[6].ToString();
                        entity.Siren = siren;
                        string isLeader = dr[7].ToString();
                        if (isLeader == "是")
                            entity.IsLeader = 1;
                        else
                            entity.IsLeader = 0;
                        string idNumber = dr[8].ToString();
                        entity.IDNumber = idNumber;

                        if (birthday == "" && idNumber != "")
                        {
                            if (idNumber.Length == 18)
                            {
                                string year = idNumber.Substring(6, 4);
                                string mm = idNumber.Substring(10, 2);
                                string dd = idNumber.Substring(12, 2);

                                DateTime ndt = new DateTime(int.Parse(year), int.Parse(mm), int.Parse(dd));
                                entity.Birthday = ndt;
                            }
                        }

                        string type = dr[9].ToString();
                        int value = 0;
                        if (type == "民警")
                            value = 1;
                        else if (type == "武警")
                            value = 2;
                        else
                            value = 3;
                        entity.Type = value;

                        string office = dr[10].ToString();
                        entity.Office = office;
                        string responsibilities = dr[11].ToString();
                        entity.Responsibilities = responsibilities;
                        string tel = dr[12].ToString();
                        entity.Tel = tel;
                        string mobilePhone = dr[13].ToString();
                        entity.MobilePhone = mobilePhone;
                        string virtualNumber = dr[14].ToString();
                        entity.VirtualNumber = virtualNumber;
                        string email = dr[15].ToString();
                        entity.Email = email;
                        string roomNumber = dr[16].ToString();
                        entity.RoomNumber = roomNumber;
                        string diskSize = dr[17].ToString();
                        if (diskSize != "")
                            entity.DiskSize = int.Parse(diskSize);
                        string deptTelephone = dr[18].ToString();
                        entity.DeptTelephone = deptTelephone;
                        string address = dr[19].ToString();
                        entity.Address = address;

                        if (id == "用户编号" && name == "用户姓名")
                        {

                        }
                        else
                        {
                            //if (!bllUserArchives.Exists(entity.Id))
                            //{
                            //    //导入
                            //    if (bllUserArchives.Add(entity))
                            //        count1++;
                            //}
                            //else
                            //{
                            //    //导入
                            //    if (bllUserArchives.Update(entity))
                            //        count2++;
                            //}

                            UserList_Entity model = new UserList_Entity();
                            model.UserAddress = entity.Address;
                            model.UserAge = 30;
                            model.UserCreateTime = DateTime.Now;
                            model.UserDetail = "";
                            model.UserEmail = entity.Email;
                            model.UserIcon = "4541b115fca4f72bc0b4";
                            model.UserId = entity.Id;
                            model.UserLevel = 1;
                            model.UserMobile = entity.MobilePhone;
                            model.UserNo = entity.Id;
                            model.UserPass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("111", "MD5");
                            model.UserRemark = "";
                            model.UserRemark = "";
                            model.UserRole = "";
                            if (entity.Sex != "")
                                model.UserSex = int.Parse(entity.Sex);
                            else
                                model.UserSex = 1;
                            model.UserState = 1;
                            model.UserTel = entity.Tel;
                            model.UserTName = entity.Name;
                            model.UserWebSite = "";

                            if (int.Parse(entity.IsLeader.ToString()) == 1)
                                model.UserZW = "领导";
                            else
                                model.UserZW = "";


                            if (bllUserList.Add(model))
                            {
                                WorkGroupUserList_Entity model2 = new WorkGroupUserList_Entity();

                                model2.UserNo = model.UserNo;
                                model2.WId = Guid.NewGuid().ToString();
                                model2.WorkGroupId = entity.dept.ToString();
                                if (model2.WorkGroupId != "0")
                                    model2.WorkGroupId = "1";
                                bllWorkGroupUserList.Add(model2);
                            }
                        }
                    }



                   
                }
            }
            catch (Exception ex) { }

            MessageBox.Show("共计添加用户记录:" + count1 + "条，更新用户记录:" + count2);

        }
    }
}
